﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;
using System.Collections;
using System.IO;
using TravianHelper.hero;

namespace TravianHelper.village
{
    public partial class VillageForm : Form
    {
        private Village village;
        private bool addNew;
        public void setVillage(Village village)
        {
            this.village = village;
            textBoxVillageName.Text = village.name;
            textBoxX.Text = village.x.ToString();
            textBoxY.Text = village.y.ToString();
            textBoxVillageId.Text = village.id;          
            updateListViewTroop(village.troops);
        }
        public VillageForm(bool addNew)
        {
            this.addNew = addNew;
            InitializeComponent();
        }
        private void VillageForm_Load(object sender, System.EventArgs e)
        {
            toolTip1.SetToolTip(this.textBoxVillageId, "This is the id in the link. For example: http://ts4.travian.com/karte.php?newdid=91695 ");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            village.name = textBoxVillageName.Text;
            village.x = Convert.ToInt32(textBoxX.Text);
            village.y = Convert.ToInt32(textBoxY.Text);
            village.id = textBoxVillageId.Text;
            if (addNew)
            {
                TravianApplicationContext.villages.Add(village);
            }
            TravianApplicationContext.mainForm.reloadComboBoxVillage();
            labelSaveStatus.Text = "Village saved";
        }

      

        private void updateListViewTroop(ArrayList troops)
        {
            listViewTroop.Items.Clear();
            foreach (Troop troop in troops)
            {
                listViewTroop.Items.Add(new ListViewItem(new string[] { troop.id.ToString(), troop.name, troop.maxDistance.ToString(), troop.stay.ToString() }));
            }
        }

        private void buttonAddTroop_Click(object sender, EventArgs e)
        {        
            string[] tmp = comboBoxTroop.SelectedItem.ToString().Split('_');
            Troop troop = new Troop();
            troop.id = Convert.ToInt32(tmp[0]);
            troop.name = tmp[1];
            troop.attack = Convert.ToInt32(tmp[2]);
            troop.carry = Convert.ToInt32(tmp[3]);
            troop.speed = Convert.ToInt32(tmp[4]);
          //  troop.number_each_wave = Convert.ToInt32(textBoxNEachWave.Text);
           // troop.minNumberEachWave = Convert.ToInt32(textBoxMinTroop.Text);
            troop.stay = Convert.ToInt32(textBoxTroopStay.Text);
            troop.maxDistance = Convert.ToInt32(textBoxMaxDistance.Text);
            village.troops.Add(troop);
            updateListViewTroop(village.troops);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(textBoxTargetX.Text);
            int y = Convert.ToInt32(textBoxTargetY.Text);
            Zone zone = new Zone();
            zone.x = x;
            zone.y = y;
            zone.distance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
          
        }

        
        delegate void AddListViewCallback(string[] items);

        private void listViewTroop_SelectedIndexChanged(object sender, EventArgs e)
        {
 
            if (listViewTroop.SelectedItems.Count > 0)
            {
                village.troops.Clear();
                listViewTroop.SelectedItems[0].Remove();
                for (int i = 0; i < listViewTroop.Items.Count; i++)
                {
                    Troop troop = new Troop();
                    troop.id = Convert.ToInt32(listViewTroop.Items[i].SubItems[0].Text);
                    troop.name = listViewTroop.Items[i].SubItems[1].Text;
                    troop.maxDistance = Convert.ToInt32(listViewTroop.Items[i].SubItems[2].Text);
                    troop.stay = Convert.ToInt32(listViewTroop.Items[i].SubItems[3].Text);
                    
                    village.troops.Add(troop);
                }
            }
        }

        private void buttonRemoveVillage_Click(object sender, EventArgs e)
        {
            TravianApplicationContext.villages.Remove(village);
            TravianApplicationContext.mainForm.reloadComboBoxVillage();
            this.Close();
        }
        /*
        private void buttonImport_Click(object sender, EventArgs e)
        {
            string[] tmp = comboBoxTroopType.SelectedItem.ToString().Split('_');
            int id = Convert.ToInt32(tmp[0]);
            int n = Convert.ToInt32(textBoxTroopNumber.Text);
            for (int i = 0; i < village.oasis.Count; i++)
            {
                Zone zone = (Zone)village.oasis[i];
                if (zone.defend == 0)
                {
                    TravianApplicationContext.adapter.addToFarmList(textBoxLinkAddFarm.Text, zone.x, zone.y, id, n);
                }
            }
            MessageBox.Show("Farmlist added");
        }
        */
        private void VillageForm_Load_1(object sender, EventArgs e)
        {

        }

        private void buttonCalDef_Click(object sender, EventArgs e)
        {
           // village.calculateDefend();
            //updateListViewTarget(village.oasis);
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string filename = openFileDialog1.FileName;
                    StreamReader reader = new StreamReader(filename);
                    string line;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine();
                        string[] tmp = line.Split(',');
                        Zone zone = new Zone();
                        zone.x = Convert.ToInt32(tmp[0]);

                        zone.y = Convert.ToInt32(tmp[1]);
                        zone.distance = Math.Sqrt((zone.x - village.x) * (zone.x - village.x) + (zone.y - village.y) * (zone.y - village.y));
                       // village.oasis.Add(zone);
                    }
                    //village.oasis.Sort(new ZoneComparer());
                    //updateListViewTarget(village.oasis);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File format error");
                }
            }
        }
                
    }
}
